﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;
using System.Windows.Forms;
using System.Threading;

namespace Ksamuelsen.Office.Outlook.Addin.VDialer {
    public partial class VDialerDialForm : Form {
        private static string _statusReady   = Properties.Resources.DIALER_STATUS_READY;
        private static string _statusInit    = Properties.Resources.DIALER_STATUS_INIT;
        private static string _statusDialing = Properties.Resources.DIALER_STATUS_DIALING;

        private Dialer _dialer;
        private bool _autoClose;
        private bool _wait;

        public VDialerDialForm() {
            InitializeComponent();
        }

        public VDialerDialForm(Dialer dialer, bool autoClose, bool wait) : this(){
            _dialer    = dialer;
            _autoClose = autoClose;
            _wait      = wait;

            Text = String.Format(Text, VDialerConfiguration.Instance.DisplayNumber);
            SetStatus(_statusReady);
            _lblContactValue.Text = dialer.ContactInfo.ContactItem.FileAs;
            _cmbNumber.Items.AddRange(dialer.ContactInfo.PhoneNumbers.ToArray());
            _cmbNumber.SelectedIndex = dialer.ContactInfo.SelectedIndex;
        }

        private void OnBtnClose(object sender, EventArgs e) {
            this.Close();
            this.Dispose();
            _dialer = null;
        }

        private void SetStatus(string status) {
            if (IsDisposed)
                return;

            if (InvokeRequired) {
                BeginInvoke(new MethodInvoker(delegate() { SetStatus(status); }));
            } else {
                _lblStatusText.Text = status;

            }
        }

        private void EnableControls(bool enable) {
            if (IsDisposed)
                return;

            if (InvokeRequired) {
                BeginInvoke(new MethodInvoker(delegate() { EnableControls(enable); }));
            } else {
                _cmbNumber.Enabled = enable;
                _btnRedial.Enabled = enable;
            }

        }

        private void OnBtnCall(object sender, EventArgs e) {
            if (_cmbNumber.SelectedItem != null) {
                EnableControls(false);
                SetStatus(_statusInit);
                Thread t = new Thread(new ParameterizedThreadStart(Dial));
                t.Start(_cmbNumber.SelectedIndex);
            }
        }

        private void Dial(object o) {
            _dialer.ContactInfo.SelectedIndex = (int)o;
            SetStatus(String.Format(_statusDialing, _dialer.ContactInfo.SelectedItem));
            _dialer.InitiateCall();
            SetStatus(_dialer.Status);
            EnableControls(true);

            if ((_autoClose) && (!_dialer.Error))
                Invoke(new MethodInvoker(delegate() { OnBtnClose(this, null); }));
        }

        private void OnFormShown(object sender, EventArgs e) {
            if (!_wait)
                OnBtnCall(this, null);
        }
    }
}
